import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import ElementUI from 'element-ui' 

Vue.use(Router)
Vue.use(ElementUI)



const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: require('@/components/home').default
    },
    {
      path: '*',
      redirect: '/'
    },{
      path: '/law',
      name: 'Law',
      component: require('@/components/law/law').default
    },{
      path: '/Advice',
      name: 'Advice',
      component: require('@/components/Advice/Advice').default
    },{
      path: '/print',
      name: 'Print',
      component: require('@/components/print/print').default
    },{
      path: '/lawserch:type',
      name: 'LawSerch',
      component: require('@/components/lawserch/lawserch').default
    },{
      path: '/recommend',
      name: 'Recommend',
      component: require('@/components/recommend/recommend').default
    },{
      path: '/demand',
      name: 'Demand',
      component: require('@/components/demand/demand').default
    },{
      path: '/user',
      name: 'User',
      component: require('@/components/user/user').default,
      children:[{
        path:'/userwork',
        name:'UserWork',
        props: true,
        component: require('@/components/user/user/UserWork').default
      },{
        path:'/userinfo',
        name:'UserInfo',
        component: require('@/components/user/user/UserInfo').default
      },{
        path:'/userpay',
        name:'UserPay',
        component: require('@/components/user/user/UserPay').default
      },{
          path:'/userpay_end',
          name:'UserPay_end',
          component: require('@/components/user/user/UserPay_end').default
      },{
        path:'/useradv',
        name:'UserAdv',
        component: require('@/components/user/user/UserAdv').default
      }]
    },

  ],
   
 
});
 // 注册导航守卫(回调函数)
    router.beforeEach((to, from, next) => {
         //找到需要判断的页面
        const nextRoute = ['User', 'LawSerch','UserPay','Print'];
        let token = window.localStorage.getItem('access_token');
        if(nextRoute.indexOf(to.name) >= 0){
            if(token!=undefined){
               next()
           }else{
        //弹框
       // 这里不是Vue实例 无法通过this访问 但是可以访问到Vue构造函数 通过原型访问
        Vue.prototype.$message.error('请先登录!')
        router.push('/home')
         // 打回登录页 也可以使用next 进行路由跳转
          //next('/home');
      }
    }else{
      next()
    }
  });
 export default router;